class SessionStorage {
  setItem(key: string, value: any) {
    window.sessionStorage.setItem(key, JSON.stringify(value));
  }
  getItem(key: string) {
    return JSON.parse(window.sessionStorage.getItem(key) || "");
  }
  clear() {
    sessionStorage.clear();
  }
}

class LocalStorage {
  set(key: string, value: any) {
    window.localStorage.setItem(key, JSON.stringify(value));
  }
  get(key: string) {
    const obj = window.localStorage.getItem(key);
    return obj ? JSON.parse(obj) : null;
  }
  clear() {
    window.localStorage.clear();
  }
}

class Cookie {
  set(key: string, value: string, expires?: Date) {
    const expiresStr = expires ? `; expires=${expires.toUTCString()}` : "";
    document.cookie = `${key}=${value}${expiresStr}; path=/`;
  }
  get(key: string) {
    const cookie = document.cookie.match(`(^|;)\\s*${key}\\s*=\\s*([^;]+)`);
    return cookie ? decodeURIComponent(cookie[2]) : null;
  }
  remove(key: string) {
    this.set(key, "", new Date(0));
  }
}

export const sessionStorage = new SessionStorage();
export const localStorage = new LocalStorage();
export const cookieUtil = new Cookie();
